package com.supreme.studyserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonrocketmq.annotation.LoggerRecord;
import com.supreme.studyserver.annotation.MissingYou;
import com.supreme.studyserver.entity.*;
import com.supreme.studyserver.mapper.CpuMapper;
import com.supreme.studyserver.mapper.ERPRoleMapper;
import com.supreme.studyserver.mapper.OracleTestMapper;
import com.supreme.studyserver.service.MysqlSecondaryService;
import com.supreme.studyserver.service.MysqlTestService;
import com.supreme.studyserver.threadstore.ThreadStore;
import org.springframework.hateoas.EntityModel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping(value = "mybatis")
public class MybatisController {

    @Resource
    private MysqlTestService mysqlTestService;

    @Resource
    private MysqlSecondaryService mysqlSecondaryService;

    @Resource
    private CpuMapper cpuMapper;

    @Resource
    private OracleTestMapper oracleTestMapper;

    @Resource
    private ERPRoleMapper erpRoleMapper;

    @LoggerRecord
    @GetMapping(value = "selectTestList")
    public AjaxResp selectTestList(MysqlTest test) {
        final List<MysqlTest> selectList = mysqlTestService.selectTestList(test);
        return AjaxResp.instance(selectList);
    }

    @GetMapping(value = "selectTestList2")
    public AjaxResp selectTestList(@RequestParam Map<String, String> test) {
        final List<MysqlTest> selectList = mysqlTestService.selectTestList2(test);
        return AjaxResp.instance(selectList);
    }

    @LoggerRecord
    @GetMapping(value = "testList")
    public AjaxResp testList(@RequestParam(value = "firstName", required = false) String firstName) {
        LambdaQueryWrapper<MysqlTest> queryWrapper = Wrappers.lambdaQuery(MysqlTest.class);
        Optional.ofNullable(firstName).ifPresent(s -> queryWrapper.eq(MysqlTest::getFirstName, s));
        final List<MysqlTest> selectList = mysqlTestService.testList(queryWrapper);
        return AjaxResp.instance(selectList);
    }

    @PostMapping(value = "insertTest")
    public AjaxResp insertTest(@RequestBody MysqlTest test) {
        int result = mysqlTestService.insertTest(test);
        return AjaxResp.instance(result);
    }

    @PostMapping(value = "insertRandomTest")
    public AjaxResp insertRandomTest() {
        int result = mysqlTestService.insertRandomTest();
        return AjaxResp.instance(result);
    }

    @PostMapping(value = "batchInsertLoopInsert/{insertCount}")
    public AjaxResp batchInsertLoopInsert(@PathVariable(value = "insertCount") Long insertCount) {
        int result = mysqlTestService.batchInsertLoopInsert(insertCount);
        return AjaxResp.instance(result);
    }

    @PostMapping(value = "batchInsertFromForEach/{insertCount}")
    public AjaxResp batchInsertFromForEach(@PathVariable(value = "insertCount") Long insertCount) {
        int result = mysqlTestService.batchInsertFromForEach(insertCount);
        return AjaxResp.instance(result);
    }

    @PostMapping(value = "batchInsertFromSqlSession/{insertCount}")
    public AjaxResp batchInsertFromSqlSession(@PathVariable(value = "insertCount") Long insertCount) {
        int result = mysqlTestService.batchInsertFromSqlSession(insertCount);
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "secondaryList")
    public AjaxResp secondaryList() {
        final List<Thing> secondaryList = mysqlSecondaryService.secondaryList();
        return AjaxResp.instance(secondaryList);
    }

    @GetMapping(value = "oracleTestList")
    public AjaxResp oracleTestList() {
        final List<OracleTest> oracleTestList = oracleTestMapper.selectList(Wrappers.lambdaQuery(OracleTest.class).orderByAsc(OracleTest::getTestId));
        final List<JSONObject> objectJsonList = oracleTestMapper.oracleTestList();
        return AjaxResp.instance(oracleTestList).append("objectJsonList", objectJsonList);
    }

    @PostMapping(value = "oracleTestCreate")
    public AjaxResp oracleTestCreate(@RequestBody OracleTest test) {
        final int insert = oracleTestMapper.insert(test);
        return AjaxResp.instance(insert);
    }

    @MissingYou
    @GetMapping(value = "cpus")
    public AjaxResp cpuList() {
        ThreadStore.printStoreInfo();
        ThreadStore.printThreadInfo();

        final List<Cpu> selectList = cpuMapper.selectList(Wrappers.emptyWrapper());
        return AjaxResp.instance(selectList);
    }

    @GetMapping(value = "cpus/{cpuId}")
    public EntityModel<Cpu> cpuOne(@PathVariable(value = "cpuId") Long cpuId) {
        ThreadStore.printStoreInfo();
        ThreadStore.printThreadInfo();

        final LambdaQueryWrapper<Cpu> cpuQueryWrapper = Wrappers.lambdaQuery(Cpu.class)
                .eq(Cpu::getCpuId, cpuId);
        final Cpu selectOne = cpuMapper.selectOne(cpuQueryWrapper);
        EntityModel<Cpu> resource = EntityModel.of(selectOne);
        return resource;
    }

    @MissingYou
    @PostMapping(value = "createCpu")
    public AjaxResp createCpu(@RequestBody Cpu cpu) {
        int insert = cpuMapper.insert(cpu);
        return AjaxResp.instance(insert);
    }

    @GetMapping(value = "erpRoleList")
    public AjaxResp erpRoleList() {
        List<ERPRole> roleList = erpRoleMapper.selectList(Wrappers.emptyWrapper());
        return AjaxResp.instance(roleList);
    }
}
